import { Injectable } from '@nestjs/common'
import * as Redis from "ioredis"
import { RedisService as _RedisService } from 'nestjs-redis'

@Injectable()
export class RedisService {

  client: Redis.Redis

  constructor(private readonly redisService: _RedisService) {
    this.client = this.redisService.getClient()
  }

  getClient(): Redis.Redis {
    return this.client
  }

  async set(key: string, value: string, seconds?: number) {
    if (seconds) {
      return await this.client.set(key, value, 'EX', seconds)
    } else {
      return await this.client.set(key, value)
    }
  }

  async get(key: string): Promise<string> {
    const result = await this.client.get(key)
    return result ?? ''
  }

  async del(key: string) {
    await this.client.del(key)
  }

}
